﻿using POC.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace wlog.Shell
{
    public class ConfigureCommand :ICommand
    {
        string _commandName;

        public string CommandName
        {
            get { return _commandName; }
            set { _commandName = value; }
        }
        object[] _args = new object[] { "", "" };

        public object[] Args
        {
            get { return _args; }
            set { _args = value; }
        }
        public ConfigureCommand(string name, params object[] args )
        {
            _commandName = name;
            _args = args;
        }
        /// <summary>
        /// Put it in lower case to facilate dynamic invoke
        /// </summary>
        public void rewrite(string key, string val)
        {
            Refresh(key, val);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public void settings(string key, string val)
        {
            Refresh(key, val);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public void appSettings(string key, string val)
        {
            Refresh(key, val);
        }
        /// <summary>
        /// Put it in lower case to facilate dynamic invoke
        /// </summary>
        private void Refresh(string key, string val)
        {
            if (string.IsNullOrEmpty(key))
            {
                ApplicationConfiguration.ReBindFileReader("FileConfigReader");
            }
            else
            {
                string filePath = ApplicationConfiguration.Get(ConfigKeyConstant.ConfigFile);
                string nodeName = ApplicationConfiguration.Get(ConfigKeyConstant.NodeName);
                ApplicationConfiguration.BindFileReader(filePath, nodeName, "FileConfigReader");
            }
        }


        public System.Threading.Tasks.Task Execute()
        {
            try
            {
                MethodInfo methodInfo = this.GetType().GetMethod(_commandName);
                methodInfo.Invoke(this, Args);
            }
            catch (Exception sc)
            {
               throw new ArgumentNullException("Some args are missing", sc);
            }
            return null;
        }

        public Model.OutputChannel Results
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public void OnAddLine(Action<string> addLine)
        {
            throw new NotImplementedException();
        }
    }
}
